<?php
include("global.php");
printHeader(false);
session_start();

$NUMERI_TELEFONO=$_SESSION['NUMERI_TELEFONO'];
$DATA_TABLE=$_SESSION['DATA_TABLE'];
$LISTA_ALR_ENABLE_SMS=$_SESSION['LISTA_ALR_ENABLE_SMS'];
$HOUR_IN_A_DAY=$_SESSION['HOUR_IN_A_DAY'];
     
$op = @$_POST["operation"];
        
if($op == "add")
{
        $d = $_POST["day"];
        $begin =  $_POST["begindate"];
        $end =  $_POST["enddate"];
        $phone =  $_POST["phone"];
        $arr_allarmi_enable=$_POST["idAlarms"];
                
	$bi = array_search($begin,$HOUR_IN_A_DAY);
	$ei = array_search($end,$HOUR_IN_A_DAY);
        $data_start=StrToDateTime($d." ".$begin);
        $data_end=StrToDateTime($d." ".$end);
        //$arr_allarmi_enable=array(1,2,5,9);
	$mysql=new Db();
        $mysql->connect();
        $arr=$mysql->execute_select("SELECT distinct(numero_sms),nome_sms,azienda_sms FROM rubrica_sms where numero_sms='$phone';");
       
        //$data_end=StrToDateTime($d." ".$end);
        for($i = $bi; $i<$ei;$i++)
	{
		$begin=$HOUR_IN_A_DAY[$i].":00";
                $end_rel=$HOUR_IN_A_DAY[$i+1].":00";

                $key = $d . " " . $HOUR_IN_A_DAY[$i];
                $data_start=StrToDateTime($key);
                $data_end=incrementaOra($data_start,30);//incremento di 30minuti
                $data_tmp=strtotime( $data_start );
                $day_week= strftime('%w',$data_tmp);

                for ($index=0; $index<count($arr_allarmi_enable); $index++)
                {
                    $id_allarm=$arr_allarmi_enable[$index];
                    $mysql->execute("INSERT INTO rubrica_sms (numero_sms,nome_sms,azienda_sms,giorno_settimana,start_ora,end_ora,day_week,id_allarm) VALUES ('".$arr[0][0]."','".$arr[0][1]."','".$arr[0][2]."', '$d','$begin','$end_rel','$day_week',$id_allarm);");

                }
//                echo "INSERT INTO rubrica_sms (numero_sms,nome_sms,azienda_sms,start_data,end_data,giorno_settimana,start_ora,end_ora) VALUES ('".$arr[0][0]."','".$arr[0][1]."','".$arr[0][2]."','$data_start', '$data_end', '$d','$begin','$end_rel'); \n";
//               
//		if(array_search($phone,$DATA_TABLE[$key]) === FALSE)
//		{
//			array_push($DATA_TABLE[$key],$phone);
//		}
	}
        $mysql->disconnettiDB();
}

if($op == "del")
{
        $checked=$_POST['idAlarms'];
        
        $N = count($checked);
        $mysql=new Db();
        $mysql->connect();
        
        for($i=0; $i < $N; $i++)
        {
            $id_sms_table=$checked[$i];
            $query="DELETE FROM rubrica_sms WHERE id='$id_sms_table' && id_allarm <> 0";
            $mysql->execute("$query");
        }
        $mysql->disconnettiDB();
        
//	$key = $d ." " . $begin;
//        $data_start=StrToDateTime($d." ".$begin);
//        $data_tmp=strtotime( $data_start );
//        $day_week= strftime('%w',$data_tmp);
//
//        //$data_end=StrToDateTime($d." ".$end);
//        $mysql=new Db();
//        $mysql->connect();
//        $mysql->execute("DELETE FROM rubrica_sms WHERE numero_sms='$phone' && start_ora='$begin' && day_week='$day_week' && id_allarm='$id_allarm';");
//        
//        $mysql->disconnettiDB();
//	print $key;
//		while(array_search($phone,$DATA_TABLE[$key]) !== FALSE)
//		{
//				unset($DATA_TABLE[$key][array_search($phone,$DATA_TABLE[$key])]);
//		}
}

$arr=explode("/",dirname($_SERVER['PHP_SELF']));
$url = "http://" . $_SERVER['SERVER_NAME'] . "/Calendario.php";
header( "Location: $url" ) ;

?>
